var oPage = {
	oThis: null, // 当前对象
	oSearch: null, // 搜索框对象

	oScrollTbody: null, // 滚动轴

	current: 1, // 当前页
	size: 20,
	paging: null, // 分页对象
	list: [],

	init: function () {
		this.initState();
		this.initEvent();
	},

	initState: function () {
		var _this = this;

		this.oThis = $('.apply-list-wp');
		this.oSearch = this.oThis.find('.search-box');
		this.oTbody = this.oThis.find('.com-table .com-table-body tbody');

		// 分页
		this.paging = new ThePaging('#applyListPaging', {
			currentChange: function (current) {
				_this.current = current;
				_this.getData();
			}
		})

		this.getData();
	},

	initEvent: function () {
		var _this = this;

		// 新增
		this.oThis.find('.main .header .add-btn').on('click', function () {
			oApplyDetailDialog.show();
		})

		// 查询
		this.oSearch.find('.search-btn').on('click', function () {
			_this.search();
		})

		// 表格查看点击
		this.oThis.find('.com-table-body').on('click', '.see-btn', function () {
			var oBtn = $(this);
			var oTr = oBtn.parent().parent();
			var idx = oTr.attr('idx');
			var oDetail = _this.list[idx];
			pb.storage.set('apply-detail', oDetail);
			window.open('./apply-see.html');
		})

		// 表格修改点击
		this.oThis.find('.com-table-body').on('click', '.edit-btn', function () {
			var oBtn = $(this);
			if (oBtn.hasClass('disabled')) {
				return;
			}
			var oTr = oBtn.parent().parent();
			var idx = oTr.attr('idx');
			oApplyDetailDialog.show(_this.list[idx]);
		})

		// 提交点击
		this.oThis.find('.com-table-body').on('click', '.tj-btn', function () {
			var oBtn = $(this);
			if (oBtn.hasClass('disabled')) {
				return;
			}
			var oTr = oBtn.parent().parent();
			pb.confirm('此操作将提交该条数据, 是否继续?').then(function () {
				var idx = oTr.attr('idx');
				_this.apply(_this.list[idx].id);
			});
		})

		// 表格审批点击
		this.oThis.find('.com-table-body').on('click', '.examine-btn', function () {
			var oBtn = $(this);
			if (oBtn.hasClass('disabled')) {
				return;
			}
			var oTr = oBtn.parent().parent();
			var idx = oTr.attr('idx');
			applyExamineDialog.show(_this.list[idx]);
		})

		// 删除点击
		this.oThis.find('.com-table-body').on('click', '.del-btn', function () {
			var oBtn = $(this);
			if (oBtn.hasClass('disabled')) {
				return;
			}
			var oTr = oBtn.parent().parent();
			pb.confirm('此操作将永久删除该数据, 是否继续?').then(function () {
				var idx = oTr.attr('idx');
				_this.delete(_this.list[idx].id);
			});
		})
	},

	// 查询第一页
	search: function () {
		this.paging.setCurrent(1);
	},
	// 查询数据
	getData: function () {
		var _this = this;
		$.ajax({
			type: 'get',
			url: pb.api + '/baImageApply/listData',
			data: {
				applyNo: this.oSearch.find('.applyNo').val(),
				patientNameSpell: this.oSearch.find('.patientNameSpell').val(),
				inspectionCode: this.oSearch.find('.inspectionCode').val(),
				imageCode: this.oSearch.find('.imageCode').val(),
				outpatientCode: this.oSearch.find('.outpatientCode').val(),
				hospitalizedCode: this.oSearch.find('.hospitalizedCode').val(),
				patientName: this.oSearch.find('.patientName').val(),
				applyDateStart: this.oSearch.find('.start-time').val(),
				applyDateEnd: this.oSearch.find('.end-time').val(),
				applyState: this.oSearch.find('.applyState').val(),
				pageNo: this.current,
				pageSize: this.size
			},
			success: function (res) {
				var list = _this.formatData(res.page.list);
				_this.list = list;
				_this.setTbody(list);
				_this.paging.setTotal(res.page.count);
			}
		})
	},
	// 格式化
	formatData: function (list) {
		list.forEach(function (item) {
			// 状态
			item.applyStateText = pb.getNameByCode(item.applyState, pb.applyStateList);
			// 制作状态
			item.makingStateText = pb.getNameByCode(item.makingState, pb.makingStateList);
		})
		return list;
	},

	setTbody: function (list) {
		var html = '';
		if (list.length) {
			list.forEach((item, index) => {
				html += '<tr idx="' + index + '">' +
							'<td>' + ((this.current-1)*this.size+index+1) + '</td>' +
							'<td title="' + (item.applyNo || '') +'">' + (item.applyNo || '-') + '</td>' +
							'<td title="' + (item.applyTime || '') +'">' + (item.applyTime || '-') + '</td>' +
							'<td title="' + (item.patientName || '') +'">' + (item.patientName || '-') + '</td>' +
							'<td title="' + (item.sex || '') +'">' + (item.sex || '-') + '</td>' +
							'<td title="' + (item.age || '') +'">' + (item.age || '-') + '</td>' +
							'<td title="' + (item.patientTypeName || '') +'">' + (item.patientTypeName || '-') + '</td>' +
							'<td title="' + (item.deptName || '') +'">' + (item.deptName || '-') + '</td>' +
							'<td title="' + (item.doctorName || '') +'">' + (item.doctorName || '-') + '</td>' +
							'<td title="' + (item.partName || '') +'">' + (item.partName || '-') + '</td>' +
							'<td title="' + (item.checkPointMethod || '') +'">' + (item.checkPointMethod || '-') + '</td>' +
							'<td title="' + (item.inspectionCode || '') +'">' + (item.inspectionCode || '-') + '</td>' +
							'<td title="' + (item.imageCode || '') +'">' + (item.imageCode || '-') + '</td>' +
							'<td title="' + (item.outpatientCode || '') +'">' + (item.outpatientCode || '-') + '</td>' +
							'<td title="' + (item.hospitalizedCode || '') +'">' + (item.hospitalizedCode || '-') + '</td>' +
							'<td title="' + (item.equipmentDetail || '') +'">' + (item.equipmentDetail || '-') + '</td>' +
							'<td title="' + (item.applyStateText || '') +'">' + (item.applyStateText || '-') + '</td>' +
							'<td title="' + (item.makingStateText || '') +'">' + (item.makingStateText || '-') + '</td>' +
							'<td>' +
								'<button class="see-btn">查看</button>' +
								'<button class="edit-btn' + ([0].indexOf(item.applyState) != -1 ? '' : ' disabled') + '">修改</button>' +
								'<button class="tj-btn' + ([0].indexOf(item.applyState) != -1 ? '' : ' disabled') + '">提交</button>' +
								'<button class="examine-btn' + ([0,4].indexOf(item.applyState)  ? '' : ' disabled') + '">审批</button>' +
								'<button class="del-btn' + ([0].indexOf(item.applyState) != -1 ? '' : ' disabled') + '">删除</button>' +
							'</td>' +
						'</tr>';
			})
		} else {
			html = '<tr class="empty"><td>暂无数据</td></tr>';
		}
			
		this.oTbody.html(html);
	},

	// 提交
	apply: function (id) {
		var _this = this;
		$.ajax({
			type: 'get',
			url: pb.api + '/baImageApply/apply?id=' + id,
			success: function (res) {
				if (res.result == 'true') {
					Qmsg.success('提交成功');
					_this.getData();
				} else {
					Qmsg.error(res.message);
				}
			}
		})
	},

	// 删除
	delete: function (id) {
		var _this = this;
		$.ajax({
			type: 'get',
			url: pb.api + '/baImageApply/delete?id=' + id,
			success: function (res) {
				if (res.result == 'true') {
					Qmsg.success('删除成功');
					_this.getData();
				} else {
					Qmsg.error(res.message);
				}
			}
		})
	}

}


$(function () {
	oPage.init();
})

